home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / screen-profiles / updates-available < prev    next >
Encoding:
Text File  |  2009-06-02  |  3.2 KB  |  98 lines

  1. #!/bin/sh -e
  2. #
  3. #    updates-available: calculate and cache the number of updates available
  4. #    Copyright (C) 2008 Canonical Ltd.
  5. #
  6. #    Authors: Dustin Kirkland <kirkland@canonical.com>
  7. #
  8. #    This program is free software: you can redistribute it and/or modify
  9. #    it under the terms of the GNU General Public License as published by
  10. #    the Free Software Foundation, version 3 of the License.
  11. #
  12. #    This program is distributed in the hope that it will be useful,
  13. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #    GNU General Public License for more details.
  16. #
  17. #    You should have received a copy of the GNU General Public License
  18. #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19.  
  20. # expire the cache in X seconds; 1 hour by default
  21. EXPIRATION=3600
  22.  
  23. print_updates() {
  24.     u=$1
  25.     s=$2
  26.     if [ -n "$u" ]; then
  27.         if [ "$u" -gt 0 ]; then
  28.             printf "\005{=b rW}%d!" "$u"
  29.             if [ -n "$s" ]; then
  30.                 if [ "$s" -gt 0 ]; then
  31.                     printf "!"
  32.                 fi
  33.             fi
  34.             printf "\005{-} "
  35.         fi
  36.     fi
  37.     exit 0
  38. }
  39.  
  40. cache=/var/run/updates-available
  41. mycache=$HOME/.screen-profiles/updates-available
  42. now=`date +%s`
  43. cache_timestamp=`stat -c "%Y" $cache 2>/dev/null || echo 0`
  44. mycache_timestamp=`stat -c "%Y" $mycache 2>/dev/null || echo 0`
  45. diff=`expr $now - $cache_timestamp`
  46. u=
  47. # If global updates-available cache is present, and newer than mycache, and
  48. # within expiration, use it, and cache it (to preserve across reboots).
  49. # Only available in Jaunty+.
  50. if [ -r $cache -a $cache_timestamp -gt $mycache_timestamp -a $diff -lt $EXPIRATION ]; then
  51.     u=`grep -m 1 "^[0-9]" $cache | sed "s/\s.*$//"`
  52.     s=`grep -m 2 "^[0-9]" $cache | tail -n 1 | sed "s/\s.*$//"`
  53.     cp -a "$cache" "$mycache"
  54.     print_updates $u $s
  55. fi
  56.  
  57. # If the user's updates-available cache is present, and less than an hour old,
  58. # use it.  (The "hour" part should be configurable)
  59. if [ -r $mycache -a -O $mycache ]; then
  60.     diff=`expr $now - $mycache_timestamp`
  61.     if [ $diff -lt $EXPIRATION ]; then
  62.         print_updates `cat $mycache`
  63.     fi
  64. else
  65.     # Otherwise, let's quickly clear the cache, and then recreate it with
  66.     # a really old timestamp (so that it get's updated on next run)
  67.     # and exit immediately
  68.     rm -f $mycache
  69.     touch -t 197001010000 $mycache
  70.     exit 0
  71. fi
  72.  
  73. # If we make it to this point, we actually have to do hard computational
  74. # work to calculate updates.  Let's try to be "nice" about it:
  75. renice 10 $$ >/dev/null 2>&1 || true
  76. ionice -c3 -p $$ >/dev/null 2>&1 || true
  77.  
  78. # These are very computationally intensive processes.
  79. # Background this work, have it write to the cache files,
  80. # and let the next cache check pick up the results.
  81.  
  82. if [ -x /usr/lib/update-notifier/apt-check ]; then
  83.     # If apt-check binary exists, use it
  84.     /usr/lib/update-notifier/apt-check 2>&1 | tail -n 1 | sed "s/;/ /" > $mycache &
  85. elif [ -x /usr/bin/apt-get ]; then
  86.     # If apt-get exists, use it
  87.     /usr/bin/apt-get -s -o Debug::NoLocking=true upgrade | grep -c ^Inst > $mycache &
  88. elif [ -x /usr/bin/zypper ]; then
  89.     # If zypper exists, use it
  90.     /usr/bin/zypper --no-refresh lu --best-effort | grep 'v |' | wc -l > $mycache &
  91. elif [ -x /usr/bin/yum ]; then
  92.     # If yum exists, use it
  93.     /usr/bin/yum list updates | grep -c "updates" > $mycache &
  94. else
  95.     # If we're here, we have no idea
  96.     print_updates "?"
  97. fi
  98.